package com.crm.system.service.impl.index;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.FileUpload;
import com.crm.model.entity.sys.News;
import com.crm.model.entity.sys.Notify;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.index.NewsQO;
import com.crm.model.qo.index.NotifyQO;
import com.crm.model.vo.announcement.AnnouncementVO;
import com.crm.model.vo.announcement.NewsVO;
import com.crm.service.common.FileUploadDbService;
import com.crm.service.index.NewsDbService;
import com.crm.system.service.index.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsDbService newsDbService;

    @Autowired
    FileUploadDbService fileUploadDbService;

    @Override
    public List<NewsVO> getImgList() {
        List<News> imgList = newsDbService.getImgList();
        List<NewsVO> hashMaps = new ArrayList<NewsVO>();

        //定义一个固定的图片路径
        String url1 = "/images/nature-1.jpg";
        String url2 = "/images/nature-2.jpg";
        String url3 = "/images/nature-3.jpg";
        Boolean news1 = false;
        Boolean news2 = false;
        Boolean news3 = false;
        NewsVO newsVO1 = new NewsVO();
        NewsVO newsVO2 = new NewsVO();
        NewsVO newsVO3 = new NewsVO();

        //查询出所有的新闻进行赋值
        for (News news:imgList) {
            FileUpload fileUpload = fileUploadDbService.selectFileUploadById(news.getUploadId());
            if (news.getNewsOrder()==1){
                newsVO1.setAccessUrl(fileUpload.getAccessUrl());
                newsVO1.setNewsTitle(news.getNewsTitle());
                newsVO1.setNewsContent(news.getNewsContent());
                newsVO1.setNewsId(news.getNewsId());
                newsVO1.setUploadId(news.getUploadId());
                news1 = true;
            }else if(news.getNewsOrder()==2){
                newsVO2.setAccessUrl(fileUpload.getAccessUrl());
                newsVO2.setNewsTitle(news.getNewsTitle());
                newsVO2.setNewsContent(news.getNewsContent());
                newsVO2.setNewsId(news.getNewsId());
                newsVO2.setUploadId(news.getUploadId());
                news2 = true;
            }else if(news.getNewsOrder()==3){
                newsVO3.setAccessUrl(fileUpload.getAccessUrl());
                newsVO3.setNewsTitle(news.getNewsTitle());
                newsVO3.setNewsContent(news.getNewsContent());
                newsVO3.setNewsId(news.getNewsId());
                newsVO3.setUploadId(news.getUploadId());
                news3 = true;
            }
        }
        //判断序号为1的新闻数据库存没存，存在就显示出来，不存在就显示默认值
        if(news1){
            hashMaps.add(newsVO1);
        }else {
            NewsVO newsVO = new NewsVO();
            newsVO.setAccessUrl(url1);
            newsVO.setNewsTitle("");
            newsVO.setNewsContent("");
            newsVO.setNewsId("");
            hashMaps.add(newsVO);
        }
        //判断序号为2的新闻数据库存没存，存在就显示出来，不存在就显示默认值
        if(news2){
            hashMaps.add(newsVO2);
        }else {
            NewsVO newsVO = new NewsVO();
            newsVO.setAccessUrl(url2);
            newsVO.setNewsTitle("");
            newsVO.setNewsContent("");
            newsVO.setNewsId("");
            hashMaps.add(newsVO);
        }
        //判断序号为3的新闻数据库存没存，存在就显示出来，不存在就显示默认值
        if(news3){
            hashMaps.add(newsVO3);
        }else {
            NewsVO newsVO = new NewsVO();
            newsVO.setAccessUrl(url3);
            newsVO.setNewsTitle("");
            newsVO.setNewsContent("");
            newsVO.setNewsId("");
            hashMaps.add(newsVO);
        }
        return hashMaps;
    }

    @Override
    public Boolean updateNewInfo(NewsQO newsQO) {
        //先获取登录用户的信息
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        News news = new News();
        news.setNewsId(newsQO.getNewsId());
        news.setNewsTitle(newsQO.getNewsTitle());
        news.setNewsContent(newsQO.getNewsContent());
        news.setUploadId(newsQO.getUploadId());
        news.setUpdatePeopleId(userAccount.getAccountId());
        news.setUpdateTm(new Date());
        Boolean result = newsDbService.updateNewInfo(news);
        return result;
    }

    @Override
    public Boolean addNesInfo(NewsQO newsQO) {
        //先获取登录用户的信息
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        News news = new News();
        news.setNewsId(CommonUtils.getGUID());
        news.setNewsTitle(newsQO.getNewsTitle());
        news.setUploadId(newsQO.getUploadId());
        news.setNewsContent(newsQO.getNewsContent());
        news.setNewsOrder(newsQO.getNewsOrder());
        news.setDeleteStatus(Short.parseShort("0"));
        news.setCreateTm(new Date());
        news.setCreatePeopleId(userAccount.getAccountId());
        Boolean result = newsDbService.addNesInfo(news);
        return result;
    }

    @Override
    public News getNewsInfoByNewOrder(Integer order) {
        News news = newsDbService.getNewsInfoByNewOrder(order);
        return news;
    }

    @Override
    public DhPageInfo<AnnouncementVO> getNotifyList(Integer pageIndex, Integer pageSize, String searchInfo,Integer publicStatus,String sort,String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Notify> notify = newsDbService.selectNotifyBySearchInfo(searchInfo,publicStatus,sort,sortOrder);
        PageInfo<Notify> pageInfo = new PageInfo<>(notify);
        List<AnnouncementVO> result = new ArrayList<AnnouncementVO>();
        for (Notify dbItem : pageInfo.getList()) {
            result.add(new AnnouncementVO(dbItem));
        }
        return new DhPageInfo<AnnouncementVO>(pageInfo, result);
    }

    @Override
    public Boolean addNotify(NotifyQO notifyQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        BeanUtils.copyProperties(notifyQO, notify);
        Notify notifyData = this.notifyData(notify, UUID.randomUUID().toString().replaceAll("-", ""), userAccount.getUserName(), notifyQO);
        notifyData.setCreateTm(new Date());
        notifyData.setCreatePeople(userAccount.getUserName());
        notifyData.setType(2);
        Boolean result = newsDbService.addNotify(notify);
        return result;
    }

    @Override
    public Boolean deleteNotify(String id) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        notify.setNotifyId(id);
        notify.setDeleteStatus(Short.parseShort("1"));
        notify.setUpdateTm(new Date());
        notify.setUpdatePeople(userAccount.getUserName());
        Boolean result =  newsDbService.updateNotify(notify);
        return result;
    }

    @Override
    public Boolean updateNotify(NotifyQO notifyQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        Notify notifyData = this.notifyData(notify, notifyQO.getNotifyId(), userAccount.getUserName(), notifyQO);
        notifyData.setTitle(notifyQO.getTitle());
        notifyData.setNotifyContent(notifyQO.getNotifyContent());
        Boolean result = newsDbService.updateNotify(notify);
        return result;
    }

    @Override
    public Notify selectNotifyById(String id) {
        Notify notify = newsDbService.selectNotifyById(id);
        return notify;
    }


    /**
     * info 提取公共数据
     */
    private Notify notifyData(Notify notify, String id, String userName, NotifyQO notifyQO) {
        notify.setNotifyId(id);
        notify.setUpdateTm(new Date());
        notify.setUpdatePeople(userName);
        notify.setUploadId(notifyQO.getFileUrls());
        return notify;
    }
}
